<!--
 * @Author: dingxiao
 * @Date: 2025-6-19 09:57:52
 * @Description: 调研工作开展情况表单
-->
<script>
import { CommonBusinessForm } from 'jdf-common-biz';
import { timeConvert } from 'jdf-util';
import ResearchWorkPlanService from '@/api/domains/researchWorkPlan/ResearchWorkPlanService';
import { CANNOT_EDIT } from '@/utils/constants';
import { htmlNormativeType, TABLE_COLUMN_NO_WARP } from '@/utils/common-form-field';
import researchFormMixins from '@/mixins/researchFormMixins';
import { has, isEmpty } from 'lodash-es';
import ResearchStatistics from '@/views/procecss-form/components/arrangement/ResearchStatistics';
import { isCompanyLeader } from '@/utils/utils';
import ResearchFormService from '@/api/domains/ResearchFormService';
import { TaskTypeEnum } from 'jdf-jssdk';

export default {
  name: 'ResearchWorkPlan',
  extends: CommonBusinessForm,
  mixins: [researchFormMixins],
  components: { ResearchStatistics },
  data() {
    return {
      // 后台服务
      service: new ResearchWorkPlanService(),
      // 需要进行主抄送重复校验的字段
      needSendDuplicateCheckFields: [],
      // 自定义校验规则
      baseRules: {},
      formData: {},
      // 表单字段布局配置
      formConfigs: [
        [
          {
            formElCode: 'contract',
            colSpan: 8,
          },
          {
            formElCode: 'createTime',
            colSpan: 8,
            formatter: (val) => timeConvert(val),
          },
          {
            formElCode: 'serialNumber',
            colSpan: 8,
          },
        ],
        [
          {
            hideColFn: () => this.isRead,
            hideColBorder: true,
            hideLabel: true,
            formElCode: 'researchLedgers',
            colSpan: 24,
            defineClassName: 'researchStatistics',
            render: (h) => h(ResearchStatistics, {
              ref: 'researchStatisticRef',
              props: {
                piid: this.piid,
                isDone: this.isDone,
                nodeId: this.nodeId,
                researchLedgers: this.formData?.researchLedgers || [],
              },
            }),
          },
        ],
        [
          {
            hideColFn: () => !this.isRead,
            hideColBorder: true,
            hideLabel: true,
            formElCode: 'workSubjects',
            elType: 'dynamicTable',
            editable: CANNOT_EDIT,
            colSpan: 24,
            attrs: {
              tableDataOption: [
                {
                  key: 'leadingUserName',
                  label: '带队领导',
                  width: 150,
                },
                {
                  key: 'researchDate',
                  label: '调研日期',
                },
                {
                  key: 'researchUnitName',
                  label: '调研对象',
                },
                {
                  key: 'leadingDepartmentName',
                  label: '牵头部门',
                },
                {
                  key: 'normativeType',
                  label: '连带性规范动作类型',
                  width: 500,
                  render: (h, { row: { showNormativeType } }) => htmlNormativeType(h, showNormativeType),
                },
              ],
            },
          },
        ],
      ],
      // 主抄送是否获取网关数据, false为不获取，true为获取
      isGateWayData: false,
      // 采用ucfg的数据字典，不采用数据库表中字典值
      isSelfDict: false,
    };
  },
  watch: {
    'formData.title': {
      handler(val) {
        this.$emit('update:processTitle', val);
      },
    },
    /**
     * 表单数据变化时触发
     */
    formData: {
      handler(val) {
        this.$emit('changeBizFormData', val);
      },
      deep: true,
    },
  },
  computed: {
    /**
     * 公司领导审批
     */
    companyLeaderApproval() {
      const { nodeId, leaderDraft } = this;
      // 当前环节是公司领导审批或者公司领导起草
      return nodeId === 'companyLeaderApproval' || leaderDraft === 'LEADER';
    },
    /**
     * 领导起草
     */
    leaderDraft() {
      return isCompanyLeader() && this.isDraftNode ? 'LEADER' : 'NORMAL';
    },
    /**
     * 是否阅办状态
     */
    isRead() {
      const { TOREAD, READED } = TaskTypeEnum;
      return [TOREAD, READED].includes(this.taskType);
    },
  },
  methods: {
    /**
     * 表单数据初始化后调用的方法
     */
    afterInit(formData) {
      this.$emit('update:documentFormTitle', formData.title);
      // 起草时根据路由参数判断是否是领导起草(formData里无此属性时证明新建表单,赋值一次即可)
      if (!has(formData, 'leaderDraft')) {
        this.bizSet({ leaderDraft: this.leaderDraft });
      }
      // 如果当前处理人是领导
      this.companyLeaderApproval && this.handleLeaderData();
    },
    /**
     * 处理领导显示数据
     */
    async handleLeaderData() {
      // 只展示自己的周计划
      const data = await new ResearchFormService().getLeaderPlanDetail({
        id: this.piid,
      });
      this.bizSet({ workSubjects: data || [] });
    },
    /**
     * 表单数据统计提交
     */
    statisticsSubmit(val) {
      this.bizSet({ workSubjects: val });
    },
    /**
     * 提交后置处理
     */
    async afterTaskSubmitInit(submitParam) {
      if (submitParam.nextNodeKey !== 'toEnd') {
        return;
      }
      const { formData, piid } = this;
      // 经过指定送结束环节后发起安排流程
      await new ResearchFormService().startResearchArrange({
        workSubjects: formData.workSubjects,
        instanceId: piid,
      });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .custom-tooltip {
  color: red;
}
::v-deep .researchStatistics-col > .researchStatistics > .el-form-item__content {
  padding: 0 !important;
}
</style>
